package util

//对数组成员进行遍历并返回新的数组
func ArrayMap [ARYROW any, RETROW any] (ary []ARYROW, c func(row ARYROW, i int) RETROW) []RETROW {
	ret := make([]RETROW, 0)
	for i, row := range ary {
		ret = append(ret, c(row, i))
	}

	return ret
}

//对数组成员进行遍历和替换
func ArrayMapRef [ARYROW any] (ary *[]ARYROW, c func(row ARYROW, i int) ARYROW) {
	for i, row := range *ary {
		(*ary)[i] = c(row, i)
	}
}

//迭代上一次计算结果
func ArrayReduce [ARYROW any, RET any] (ary []ARYROW, ret RET, c func(ret RET, row ARYROW, index int) RET) RET {
	for i, row := range ary {
		ret = c(ret, row, i)
	}

	return ret
}

//过滤数组元素
func ArrayFilter [ARYROW any] (ary []ARYROW, c func(row ARYROW, index int) bool) []ARYROW {
	ret := make([]ARYROW, 0)
	for i, row := range ary {
		if c(row, i) {
			ret = append(ret, row)
		}
	}

	return ret
}